home *** CD-ROM | disk | FTP | other *** search
/ FM Towns: Free Software Collection 9 / FM Towns Free Software Collection 9.iso / t_os / shell / igo / gosource / kiffile.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-11-16  |  3.8 KB  |  201 lines

  1. #define DEBUG 0
  2. /* 
  3.     TOWNS囲碁棋譜記録プログラム KIFFILE.C
  4.                                           1992/04/06  久保田俊也
  5.  */
  6. /*
  7.     機能
  8.         kiffileの読み込み 
  9.         kiffileの書き込み
  10.         KIF_HEADERの読み込み
  11.         KIF_HEADERの書き込み
  12.         KIF_TEの読み込み(コメント付) 
  13.         KIF_TEの書き込み(コメント付)
  14.         
  15.         記憶域の割当機能まだ未完成
  16.  */
  17. #include <stdio.h>
  18. #include <stdlib.h>
  19. #include <string.h>
  20. #include "igo.h"
  21. #include "banx.h"
  22. #include "kiffile.h"
  23. #include "title.h"
  24.  
  25. int kiffile_read(char fname[]);
  26. int kiffile_write(char fname[]);
  27. int kiftitle_read();
  28. int kiftitle_write();
  29. int kifte_write(TE_ARG kiffile_te); 
  30. char *comment_read(int comment_no);
  31. void *my_malloc(size_t size);
  32.  
  33. int te_no = 0;
  34. void *alloc;
  35. TE_ARG *te_p2;
  36. static FILE *fp;
  37.  
  38. static KIF_HV33 kif_hv33;
  39. TE_ARG kiffile2[MAX_TE_NUMBER];
  40.  
  41. int kiffile_read(char fname[])
  42. {
  43.  
  44.     if((fp=fopen(fname,"rb"))==NULL){
  45.         printf("read open error!\n");
  46.         return -1;
  47.     }
  48.  
  49. #if DEBUG
  50.     printf("kiffile read\n");
  51. #endif
  52.  
  53.     fread(&kif_hv33, sizeof(KIF_HV33), 1, fp);
  54.     
  55.     if(kif_hv33.file_id[0] == 'K' &&
  56.        kif_hv33.file_id[1] == 'I' &&
  57.        kif_hv33.file_id[2] == 'F' &&
  58.        kif_hv33.file_id[3] == '3' ){
  59.  
  60.         alloc = my_malloc(kif_hv33.te_number*sizeof(TE_ARG));
  61.         if(alloc == NULL){
  62.             printf("my_malloc error!\n");
  63.             return -1;
  64.         }
  65.         
  66.         fread(alloc, sizeof(TE_ARG), kif_hv33.te_number, fp);
  67.  
  68.     }
  69.  
  70.     kiftitle_read();
  71.     te_no = 0;
  72.     te_p2 = (TE_ARG *)alloc;
  73.     
  74.     return 0;
  75. }
  76.  
  77. static int kiftitle_read()
  78. {
  79. BAN_TYPE ban_type = { NORMAL, 19, 0};
  80.  
  81.     title_tenumber_set(kif_hv33.te_number);
  82.     title_playstart_time_set(kif_hv33.play_start);
  83.     title_playend_time_set(kif_hv33.play_end);
  84.     title_handy_set(kif_hv33.handy);
  85.     title_komiset( kif_hv33.komi_id, kif_hv33.komi_number, kif_hv33.hanmoku_id);
  86.     title_blacknameset( kif_hv33.player_black);
  87.     title_whitenameset( kif_hv33.player_white);
  88.     title_playspaceset(kif_hv33.play_space);
  89.     title_issueset( kif_hv33.vicdef_id, kif_hv33.vicdef_number);
  90.     if(kif_hv33.ver == 2){
  91.         title_bantype_set( ban_type);
  92.     }else{
  93.         title_bantype_set( kif_hv33.ban_type);
  94.     }
  95.  
  96.     return 0;
  97. }
  98.  
  99. int kiffile_comment_read()
  100. {
  101. int i, c;
  102. char comment[256];
  103.  
  104.     i=0;
  105.     while((c = getc(fp))!=EOF){
  106.         if((comment[i] = (char)c) == '\0'){
  107.             comment_set(comment);
  108.             i=0;
  109.         }else{
  110.             i++;
  111.         }
  112.     }
  113.  
  114.     if(fclose(fp)==EOF){
  115.         printf("read close error!\n");
  116.         return -1;
  117.     }
  118.  
  119.     return 0;
  120. }
  121.  
  122. int kifte_read2(TE_ARG *kiffile_te)
  123. {
  124.     *kiffile_te  = *te_p2;
  125.     te_no++;
  126.     te_p2++;
  127.     if(te_no == kif_hv33.te_number){
  128.  
  129.         my_free(alloc); 
  130.         return(1);    
  131.  
  132.     }
  133.     return(0);
  134.  
  135. }
  136.  
  137. int kiftitle_write()
  138. {
  139.  
  140.     kif_hv33.ver = title_ver_read();
  141.     kif_hv33.play_start = *title_playstart_time_read();
  142.     kif_hv33.play_end = *title_playend_time_read();
  143.     kif_hv33.handy = title_handy_read();
  144.     title_komiread( &kif_hv33.komi_id, 
  145.                     &kif_hv33.komi_number,
  146.                     &kif_hv33.hanmoku_id);
  147.     title_blacknameread( kif_hv33.player_black);
  148.     title_whitenameread( kif_hv33.player_white);
  149.     title_playspaceread( kif_hv33.play_space);
  150.     title_issueread( &kif_hv33.vicdef_id,
  151.                     &kif_hv33.vicdef_number);
  152.     kif_hv33.ban_type = *title_bantype_read();
  153.  
  154.     te_no = 0;
  155.     
  156.     return 0;
  157. }
  158.  
  159. int kifte_write(TE_ARG kiffile_te2) 
  160. {
  161.     kiffile2[te_no] = kiffile_te2;
  162.     te_no++;
  163.  
  164.     return 0;
  165. }
  166.  
  167. int kiffile_write( char fname[])
  168. {
  169. FILE *fp;
  170. int c, i, j;
  171. char *comment;
  172.  
  173.     kif_hv33.file_id[0] = 'K';
  174.     kif_hv33.file_id[1] = 'I';
  175.     kif_hv33.file_id[2] = 'F';
  176.     kif_hv33.file_id[3] = '3';
  177.     kif_hv33.te_number = te_no;
  178.     
  179.     if((fp=fopen(fname,"wb"))==NULL){
  180.             printf("write open error!\n");
  181.             return -1;
  182.     }
  183.  
  184.     fwrite(&kif_hv33, sizeof(KIF_HEADER_VER3), 1, fp);
  185.  
  186.     fwrite(&kiffile2[0], sizeof(TE_ARG), te_no, fp);
  187.  
  188.     for(i=1;(comment=comment_read(i))!=NULL;i++){
  189.         j=0;
  190.         do{
  191.             c = (int)comment[j];
  192.             putc(c, fp);
  193.             j++;
  194.         }while((char)c != '\0');
  195.     }
  196.     
  197.     fclose(fp);
  198.  
  199.     return 0;
  200. }
  201.